package com.ln.core.utils;


import org.apache.commons.codec.binary.Base64;

import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 常用加密算法工具类
 */
@SuppressWarnings("restriction")
public class EncryptUtils {

    /**
     * 用MD5算法进行加密
     * 需要加密的字符串
     *
     * @param
     * @return 加密后的字符串
     */
    public static String md5(String str) {
        return encode(str, "MD5");
    }

    /**
     * 用SHA算法进行加密
     * 需要加密的字符串
     *
     * @param
     * @return 加密后的字符串
     */
    public static String sha(String str) {
        return encode(str, "SHA");
    }

    /**
     * 用base64算法进行加密
     * 需要加密的字符串
     *
     * @param
     * @return 加密后的字符串
     */
    public static String base64Encode(String str) {
        Base64 base64 = new Base64();
        byte[] r = base64.encode(str.getBytes());
        return new String(r);
    }

    /**
     * 用base64算法进行解密
     * 需要加密的字符串
     *
     * @param
     * @return 加密后的字符串
     * @throws IOException
     */
    public static String base64Decode(String str) throws IOException {
        Base64 base64 = new Base64();
        byte[] r = base64.decode(str.getBytes());
        return new String(r);
    }

    /**
     * 加密算法
     * 需要加密的字符串
     * 加密方法
     *
     * @param
     * @param
     * @return 加密后的字符串
     */
    private static String encode(String s, String method) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] strTemp = s.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return s;
        }
    }

    public static String encode_old(String str, String method) {
        MessageDigest md = null;
        String dstr = null;
        try {
            md = MessageDigest.getInstance(method);
            md.update(str.getBytes());
            dstr = new BigInteger(1, md.digest()).toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return dstr;
    }

    public static void main(String[] args) throws IOException {
        System.out.println("md5加密:" + md5("hello"));
        System.out.println("md5加密:" + md5("中文"));

        System.out.println("sha加密:" + sha("hello"));
        System.out.println("sha加密:" + sha("中文"));

        String temp = base64Encode("hello");
        System.out.println("base64加密:" + temp);
        System.out.println("base64解密:" + base64Decode(temp));

    }
}
